package block;

import java.awt.Graphics;
import java.io.Serializable;

abstract public class LogicBlock implements Serializable {

    public int startX, startY, row, column;
    public int outputs;
    private static final long serialVersionUID = 2065639333641088198L;

    public LogicBlock(int startX, int startY, int column, int row, boolean[][] taken){
        this.startX = startX;
        this.startY = startY;
        this.row = row;
        this.column = column;
        outputs = -1;

        take(taken);
    }

    public void run(int[][] inputs){
        for(int i = startY; i < startY + row; i ++)
            if(inputs[startX][i] == -1) {
                outputs = -1;
                return;
            }
        outputs = 0;
    }

    public void draw(Graphics g, int unit, int zeroX, int zeroY){
        int startXLoc = zeroX + startX * unit;
        int startYLoc = zeroY + startY * unit;
        int width = unit * column;
        int height = unit * row;
        g.drawLine(startXLoc, startYLoc, startXLoc + width, startYLoc);
        g.drawLine(startXLoc + width, startYLoc, startXLoc + width, startYLoc + height);
        g.drawLine(startXLoc + width, startYLoc + height, startXLoc, startYLoc + height);
        g.drawLine(startXLoc, startYLoc + height, startXLoc, startYLoc);
    }

    public void invalidateBlock(int[][] cond, boolean[][] taken){
        for(int j = startY; j < startY + row; j ++)
            cond[startX + column][j] = -1;
        for(int i = startX; i < startX + column; i ++)
            for(int j = startY; j < startY + row; j ++)
                taken[i][j] = false;
    }

    abstract public void fulfill(int[] inputs);

    public void take(boolean[][] taken){
        for(int i = startX; i < startX + column; i ++)
            for(int j = startY; j < startY + row; j ++)
                taken[i][j] = true;
    }
}
